const connection = require('../app/databases');

class CommentService {
  async createComment(userId, content, momentId) {
    // console.log(userId, content, momentId);
    try {
      const statement = `INSERT INTO comment (user_id, content, moment_id) VALUES (?, ?, ?);`;
      const [result] = await connection.execute(statement, [userId, content, momentId]);

      return result;
    } catch (error) {
      return error.message;
    }
  }

  async replyComment(userId, content, momentId, commentId) {
    console.log(userId, content, momentId);
    try {
      const statement = `INSERT INTO comment (user_id, content, moment_id, comment_id) VALUES (?, ?, ?, ?);`;
      const [result] = await connection.execute(statement, [userId, content, momentId, commentId]);

      return result;
    } catch (error) {
      return error.message;
    }
  }

  async updatedComment(commentId, content) {
    try {
      const statement = `UPDATE comment SET content = ? WHERE id = ?;`;
      const [result] = await connection.execute(statement, [content, commentId]);

      return result;
    } catch (error) {
      return error.message;
    }
  }

  async removeComment(commentId) {
    try {
      const statement = `DELETE FROM comment WHERE id = ?`;
      const [result] = await connection.execute(statement, [commentId]);

      return result;
    } catch (error) {
      return error.message;
    }
  }
}

module.exports = new CommentService();